import 'package:json2dart_safe/json2dart.dart';
import 'package:pornhub_app/env/environment_service.dart';

import '../../utils/utils.dart';
import '../search/search_res_model.dart';
import '../axis_cover.dart';

class EpisodeModel {
  final String afterRecordTime;
  final String beforeRecordTime;
  final int commentNum;
  final List<String> coverImgs;
  final String desc;
  final int episodeId;
  final String recordTime;
  final String title;
  final List<EpisodeVideoModel> preVideo;

  String get cover => coverImgs.firstOrNull ?? '';
  String get beforeRecordTimeText =>
      Utils.dateFmt(beforeRecordTime, ['yyyy', '年', 'm', '月']);
  String get afterRecordTimeText =>
      Utils.dateFmt(afterRecordTime, ['yyyy', '年', 'm', '月']);

  EpisodeModel.empty() : this.fromJson({});

  bool get isEmpty => episodeId == 0;

  EpisodeModel.fromJson(Map<String, dynamic> json)
      : beforeRecordTime = json.asString('beforeRecordTime'),
        afterRecordTime = json.asString('afterRecordTime'),
        commentNum = json.asInt('commentNum'),
        coverImgs = json.asList<String>('coverImgs') ?? [],
        desc = json.asString('desc'),
        episodeId = json.asInt('episodeId'),
        recordTime = json.asString('recordTime'),
        title = json.asString('title'),
        preVideo = json.asList('preVideo', EpisodeVideoModel.toBean) ?? [];

  static dynamic toBean(dynamic json) => EpisodeModel.fromJson(json);
}

class EpisodeVideoModel extends AxisCover {
  final List<String> coverImg;
  final String desc;
  final List<String> detailImg;
  final String id;
  final String recordTime;
  final String startTime;
  final List<String> tags;
  final String title;
  final List<String> verticalImg;
  final DynamicBindVideoModel? video;

  @override
  String get hCover => coverImg.firstOrNull ?? '';
  @override
  String get vCover => verticalImg.firstOrNull ?? '';

  String get startTimeShortText =>
      Utils.dateFmt(startTime, ['m', '月', 'd', '日']);
  String get startTimeText =>
      Utils.dateFmt(startTime, ['yyyy', '年', 'mm', '月', 'dd', '日']);

  EpisodeVideoModel.fromJson(Map<String, dynamic> json)
      : coverImg = json.asList<String>('coverImg') ?? [],
        desc = json.asString('desc'),
        detailImg = json.asList<String>('detailImg') ?? [],
        id = json.asString('id'),
        recordTime = json.asString('recordTime'),
        startTime = json.asString('startTime'),
        tags = json.asList<String>('tags') ?? [],
        title = json.asString('title'),
        verticalImg = json.asList<String>('verticalImg') ?? [],
        video = json.asBean('video', DynamicBindVideoModel.toBean);

  static dynamic toBean(dynamic json) => EpisodeVideoModel.fromJson(json);
}

class DynamicBindVideoModel {
  final String authKey;
  final CdnRes? cdnRes;
  final String coverImg;
  final int height;
  final String id;
  final int playTime;
  final String resourceTitle;
  final String title;
  final String videoUrl;
  final int width;

  String get authedVideoUrl => Environment.buildAuthPlayUrlString(
        videoUrl: videoUrl,
        authKey: authKey,
        id: id,
      );
  bool get isEmpty => id.isEmpty;
  bool get isNotEmpty => !isEmpty;

  DynamicBindVideoModel.fromJson(Map<String, dynamic> json)
      : authKey = json.asString('authKey'),
        cdnRes = json.asBean<CdnRes>('cdnRes', CdnRes.toBean),
        coverImg = json.asString('coverImg'),
        height = json.asInt('height'),
        id = json.asString('id'),
        playTime = json.asInt('playTime'),
        resourceTitle = json.asString('resourceTitle'),
        title = json.asString('title'),
        videoUrl = json.asString('videoUrl'),
        width = json.asInt('width');

  static dynamic toBean(dynamic json) => DynamicBindVideoModel.fromJson(json);
}
